package stock.database;


import java.sql.*;


public class Base {
    private Connection connection;
    
    static {
        try {
            Class.forName(getDriverName()).newInstance();        
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    static String getDriverName() {
        return "com.mysql.jdbc.Driver";
    }
    
    static String getURL() {
        return "jdbc:mysql://localhost:3306/stock";
    }
    
    public Connection getConnection(){
        if (connection!=null)
            return connection;
        
        try {
            connection = DriverManager.getConnection(getURL(),"root","admin");
            return connection;
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    
    public void dispose(){
        if (connection==null)
            return;
        
        try {
            connection.close();
            connection=null;
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}